package be.kdg.bombermanunlimited.client.localPlayer;

import java.rmi.RemoteException;

import be.kdg.bombermanunlimited.client.Interfaces.ILocalPlayer;
import be.kdg.bombermanunlimited.server.objects.interfaces.IPlayer;

/**
 * proxy player: dit is een lokale buffer voor positie en andere settings. houdt
 * een instantie IPlayer bij, die gezet wordt bij de constructor. hierdoor dient
 * deze klasse als Proxy en zal zo alles met het overeenkomstige IPlayer object
 * op de server afhandelen.
 * 
 * @author pieter
 * 
 */
public class ProxyPlayer implements ILocalPlayer {
	private IPlayer player;
	private String name;
	private int bombRange;
	private int row, column;
	private boolean dead;
	boolean isLocal;
	private byte directions;

	/**
	 * velden van IPlayer worden locaal gekopieerd. IPlayer wordt zelf ook
	 * bijgehouden voor de communicatie met de server.
	 * 
	 * 
	 * @param player
	 * @param isLocal true= het gaat om de eigen speler.
	 * @throws RemoteException
	 */
	public ProxyPlayer(IPlayer player, boolean isLocal) throws RemoteException {
		this.name = player.getName();
		this.player = player;
		this.row = player.getRow();
		this.column = player.getColumn();
		this.bombRange = player.getBombRange();
		this.dead = false;
		this.isLocal = isLocal;
		directions = player.getWalkableDirections();
	}

	/**
	 * update, voornamelijk van de posities
	 */
	public void update(String name, int row, int column) {
		setName(name);
		setRow(row);
		setColumn(column);
	}


	public void incColumn() {
		column++;
		try {
			directions = player.incColumn();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void printPosition() {
		System.out.println("x=" + row + " y=" + column);
	}

	public void decColumn() {
		column--;
		try {
			directions = player.decColumn();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void decRow() {
		row--;
		try {
			directions = player.decRow();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void incRow() {
		row++;
		try {
			directions = player.incRow();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public int getBombRange() {
		return bombRange;
	}

	public String getName() {
		return name;
	}

	public void increaseBombRange() {
		this.bombRange = this.getBombRange() + 1;
		try {
			player.increaseBombRange();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void setBombRange(int b) {
		this.bombRange = b;
	}

	public void setName(String n) {
		this.name = n;
	}

	public void setPosition(int row, int column) {
		setRow(row);
		setColumn(column);
	}

	public void dropBomb() {
		try {
			player.dropBomb();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void setDead() {
		dead = true;
	}

	public boolean isDead() {
		return dead;
	}

	public boolean isCanGoUp() {
		return (directions & IPlayer.WALK_UP) > 0;
	}

	public boolean isCanGoDown() {
		return (directions & IPlayer.WALK_DOWN) > 0;
	}

	public boolean isCanGoLeft() {
		return (directions & IPlayer.WALK_LEFT) > 0;
	}

	public boolean isCanGoRight() {
		return (directions & IPlayer.WALK_RIGHT) > 0;
	}

	public boolean isLocal() {
		return isLocal;
	}

	@Override
	public void setLocation(int row, int column) {
		setRow(row);
		setColumn(column);
	}
}
